package com.tianzhu.domain;

import static javax.persistence.FetchType.EAGER;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;


import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;


/**
 * The persistent class for the USERS database table.
 * 
 */
@Entity
@Table(name="USERS")
@NamedQuery(name="User.findAll", query="SELECT u FROM User u")
public class User implements UserDetails,Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@Column(name="USERNAME",unique=true, nullable=false)
	private String username;

	@Column(name="ENABLED",nullable=false, precision=1)
	private Integer enabled;

	@Column(name="PASSWORD",nullable=false)
	private String password;
	
	
	@Column(name="CUSTOMERID")
	private Long customerid;
	
	
	public Long getCustomerid() {
		return customerid;
	}

	public void setCustomerid(Long customerid) {
		this.customerid = customerid;
	}

	//bi-directional many-to-one association to Authority
	@OneToMany(mappedBy="username",fetch = EAGER)
	private List<GroupMember> groupMembers;

	//bi-directional many-to-one association to Authority
	@OneToMany(mappedBy="user",fetch = EAGER)
	private List<Authority> authorities;

	public User() {
	}

	public String getUsername() {
		return this.username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public Integer getEnabled() {
		return this.enabled;
	}

	public void setEnabled(Integer enabled) {
		this.enabled = enabled;
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public List<Authority> getUserAuthorities() {
		return this.authorities;
	}

	public void setAuthorities(List<Authority> authorities) {
		this.authorities = authorities;
	}
	
	public List<GroupMember> getGroupMembers() {
		return this.groupMembers;
	}

	public void setGroupMembers(List<GroupMember> groupMembers) {
		this.groupMembers = groupMembers;
	}

	public Authority addAuthority(Authority authority) {
		getUserAuthorities().add(authority);
		authority.setUser(this);

		return authority;
	}

	public Authority removeAuthority(Authority authority) {
		getUserAuthorities().remove(authority);
		authority.setUser(null);

		return authority;
	}
	
		
	public List<String> getGroupAuthorities() {
		List<String> allAuthorities= new ArrayList<String>();
		for (GroupMember groupMember : getGroupMembers()) {
            
			for(GroupAuthority roupAuthority : groupMember.getGroup().getGroupAuthorities()){
				allAuthorities.add(roupAuthority.getAuthority());
			}
        }        
		return allAuthorities;
		
	}
	
	@Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Set<GrantedAuthority> grantedAuthorities = new LinkedHashSet<>();
        for (Authority role : getUserAuthorities()) {
            grantedAuthorities.add(new SimpleGrantedAuthority(role.getAuthority()));
        }
        for(String auth : getGroupAuthorities()){
        	grantedAuthorities.add(new SimpleGrantedAuthority(auth));
        }
        
        return grantedAuthorities;
    }

	@Override
	public boolean isAccountNonExpired() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isEnabled() {
		// TODO Auto-generated method stub
		return this.enabled.equals(new Integer(1));
	}

}